package com.collect.discord.javacode;

import com.collect.common.eventbus.message.EventBusMessage;
import com.collect.common.eventbus.support.EventBusTemplate;
import com.collect.common.utils.eventbus.EventBusUtils;
import com.collect.common.utils.http.HttpClientUtils;
import com.collect.common.utils.spring.SpringUtils;
import org.javacord.api.entity.message.Message;
import org.javacord.api.event.message.MessageCreateEvent;
import org.javacord.api.listener.message.MessageCreateListener;

/**
 * @ Author :hky
 * @ Date  : Created in 15:13 2022-09-19
 * @ Version discord
 * @ Decribtion : $
 */
public class RecieveMessageEvent implements MessageCreateListener {
    private static final String FLAG="WTC";
    @Override
    public void onMessageCreate(MessageCreateEvent event) {
        String proxyHost = "127.0.0.1";
        String proxyPort = "7890";
        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", proxyPort);
        // 对https也开启代理
        System.setProperty("https.proxyHost", proxyHost);
        System.setProperty("https.proxyPort", proxyPort);

        //将收集到的消息，异步保存，不影响主进程
        Message message = event.getMessage();
        EventBusTemplate eventBusTemplate = SpringUtils.getBean(EventBusTemplate.class);
        eventBusTemplate.asyncPost(new EventBusMessage(EventBusUtils.DISCORDMESSAGE,message,null));

        String messageContent = event.getMessageContent();
        if (messageContent.toUpperCase().contains(FLAG)) {
            try {
                String result= HttpClientUtils.doGet("https://www.binance.com/api/v3/ticker/price", null,null,true);
                event.getChannel().sendMessage("122");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
